package kercar.route;

import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;

import kercar.gps.GPSPosition;

public class Route implements IRoute {
	
	/************************************************************************* BLOCK
	 * 		ATTRIBUTES
	 *******************************************************************************/
	
	private GPSPosition _next;
	private ConcurrentHashMap<Integer, GPSPosition> _nodes;
	private int _currentNode;
	
	/************************************************************************* BLOCK
	 * 		CONSTRUCTOR
	 *******************************************************************************/
	
	public Route() {
		_nodes = new ConcurrentHashMap<Integer, GPSPosition>();
		_currentNode = 0;
	}
	
	/************************************************************************* BLOCK
	 * 		PUBLIC METHODS
	 *******************************************************************************/
	
	@Override
	public synchronized void addTarget(GPSPosition target) {
		_nodes.put(_nodes.size(), target);
	}

	@Override
	public synchronized void removeTarget(GPSPosition target) {
		_nodes.remove(target);
	}
	
	@Override
	public synchronized void loadNextTarget() throws NoSuchElementException {
		if(_currentNode >= _nodes.size() - 1)
			throw new NoSuchElementException();
		_next = _nodes.get(++_currentNode);
	}
	
	/************************************************************************* BLOCK
	 * 		GETTERS & SETTERS
	 *******************************************************************************/
	
	@Override
	public synchronized GPSPosition getNextTarget() {
		return _next;
	}

	@Override
	public synchronized GPSPosition getOrigin() {
		return _nodes.get(0);
	}

	@Override
	public synchronized GPSPosition getDestination() {
		return _nodes.get(_nodes.size() - 1);
	}
}
